﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace MLBService.Resources
{
    [DataContract]
    public class TeamItem
    {
        #region Properties

        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public int Year { get; set; }
        [DataMember]
        public string Nickname { get; set; }
        [DataMember]
        public UriLink Franchise { get; set; }
        [DataMember]
        public List<UriLink> Players = new List<UriLink>();
        [DataMember]
        public string League { get; set; }
        [DataMember]
        public string Division { get; set; }
        [DataMember]
        public int Finish { get; set; }
        [DataMember]
        public int? G { get; set; }
        [DataMember]
        public int? W { get; set; }
        [DataMember]
        public int? L { get; set; }
        [DataMember]
        public bool? WCWinner { get; set; }
        [DataMember]
        public bool? DivWinner { get; set; }
        [DataMember]
        public bool? LgWinner { get; set; }
        [DataMember]
        public bool? WSWinner { get; set; }
        [DataMember]
        public int? R { get; set; }
        [DataMember]
        public int? AB { get; set; }
        [DataMember]
        public int? H { get; set; }
        [DataMember]
        public int? Doubles { get; set; }
        [DataMember]
        public int? Triples { get; set; }
        [DataMember]
        public int? HR { get; set; }
        [DataMember]
        public int? BB { get; set; }
        [DataMember]
        public int? SO { get; set; }
        [DataMember]
        public int? SB { get; set; }
        [DataMember]
        public int? CS { get; set; }
        [DataMember]
        public int? HBP { get; set; }
        [DataMember]
        public int? SF { get; set; }
        [DataMember]
        public int? RA { get; set; }
        [DataMember]
        public int? ER { get; set; }
        [DataMember]
        public Double? ERA { get; set; }
        [DataMember]
        public int? CG { get; set; }
        [DataMember]
        public int? SHO { get; set; }
        [DataMember]
        public int? SV { get; set; }
        [DataMember]
        public int? IpOuts { get; set; }
        [DataMember]
        public int? HA { get; set; }
        [DataMember]
        public int? HRA { get; set; }
        [DataMember]
        public int? BBA { get; set; }
        [DataMember]
        public int? SOA { get; set; }
        [DataMember]
        public int? E { get; set; }
        [DataMember]
        public int? DP { get; set; }
        [DataMember]
        public Double? FP { get; set; }
        [DataMember]
        public int? Attendance { get; set; }
        [DataMember]
        public int? BPF { get; set; }
        [DataMember]
        public int? PPF { get; set; }
        [DataMember]
        public string Designator { get; set; }
        [DataMember]
        public string Ballpark { get; set; }

        #endregion


        public TeamItem Load(int teamId)
        {
            #region Base team stats

            using (Model.fourseamEntities context = new Model.fourseamEntities())
            {
                var team =
                    (from f in context.Franchises
                    from ft in context.FieldedTeams
                    from ftb in context.FieldedTeamBallparks
                    from bp in context.BallParks
                    from d in context.Divisions.Where(d => d.DivisionId == ft.DivisionId).DefaultIfEmpty()
                    from l in context.Leagues.Where(l => l.LeagueId == ft.LeagueId).DefaultIfEmpty()
                    from n in context.TeamNickNames

                    where f.FranchiseId == ft.FranchiseId &&
                        ft.FieldedTeamId == ftb.TeamId &&
                        ftb.BallparkId == bp.BallparkId &&
                        n.TeamNickNameId == ft.TeamNickNameId &&
                        ft.FieldedTeamId == teamId

                    select new
                    {
                        ft.FieldedTeamId,
                        l.LeagueName,
                        l.LeagueAbbr,
                        d.DivisionName,
                        d.DivisionAbbr,
                        f.FranchiseId,
                        f.FranchiseName,
                        f.FranchiseDesignator,
                        ballpark = bp.BallparkName,
                        ab = ft.AB,
                        attendance = ft.Attendance,
                        bb = ft.BB,
                        bba = ft.BBA,
                        ft.BPF,
                        ft.CG,
                        ft.CS,
                        ft.Doubles,
                        ft.DP,
                        ft.E,
                        ft.ER,
                        ft.ERA,
                        ft.Finish,
                        ft.FP,
                        ft.G,
                        ft.GAtHome,
                        ft.H,
                        ft.HA,
                        ft.HBP,
                        ft.HR,
                        ft.HRA,
                        ft.IPouts,
                        ft.L,
                        ft.PPF,
                        ft.R,
                        ft.RA,
                        ft.SB,
                        ft.SF,
                        ft.SHO,
                        ft.SO,
                        ft.SOA,
                        ft.SV,
                        ft.TeamDesignator,
                        n.NickName,
                        ft.Triples,
                        ft.W,
                        ft.WonDiv,
                        ft.WonLeague,
                        ft.WonWC,
                        ft.WonWS,
                        ft.Yr
                    }).FirstOrDefault();
                this.AB = team.ab;
                this.Attendance = team.attendance;
                this.Ballpark = team.ballpark;
                this.BB = team.bb;
                this.BBA = team.bba;
                this.BPF = team.BPF;
                this.CG = team.CG;
                this.CS = team.CS;
                this.Designator = team.FranchiseDesignator;
                this.Division = team.DivisionName;
                this.DivWinner = team.WonDiv;
                this.Doubles = team.Doubles;
                this.DP = team.DP;
                this.E = team.E;
                this.ER = team.ER;
                this.ERA = team.ERA;
                this.Finish = team.Finish;
                this.FP = team.FP;
                this.G = team.G;
                this.H = team.H;
                this.HA = team.HA;
                this.HBP = team.HBP;
                this.HR = team.HR;
                this.HRA = team.HRA;
                this.Id = team.FieldedTeamId;
                this.IpOuts = team.IPouts;
                this.L = team.L;
                this.League = team.LeagueName;
                this.LgWinner = team.WonLeague;
                this.Nickname = team.NickName;
                this.PPF = team.PPF;
                this.R = team.R;
                this.RA = team.RA;
                this.SB = team.SB;
                this.SF = team.SF;
                this.SHO = team.SHO;
                this.SO = team.SO;
                this.SOA = team.SOA;
                this.SV = team.SV;
                this.Triples = team.Triples;
                this.W = team.W;
                this.WCWinner = team.WonWC;
                this.WSWinner = team.WonWS;
                this.Year = team.Yr;

            #endregion

                this.Franchise = new UriLink(string.Format("{0}franchise/{1}", Constants.baseUrl, team.FranchiseId), team.FranchiseName);

                var q =
                    (from p in context.People
                    from a in context.Appearances
                    from ft in context.FieldedTeams
                    where p.PersonId == a.PersonId &&
                    a.TeamId == ft.FieldedTeamId &&
                    ft.FieldedTeamId == teamId
                    orderby p.LastName
                    select p).ToList();

                foreach (var itm in q)
                {
                    this.Players.Add(new UriLink(string.Format("{0}player/{1}", Constants.baseUrl, itm.PersonId), string.Format("{0} {1}", itm.FirstName, itm.LastName)));
                }
            }

            return this;
        }
    }
}